// 1.导入 express
const express = require('express');

// 2.创建应用对象
const app = express();


// 3.创建路由
app.get('/home', (req, res) => {
  res.end('hello world');
});

// 声明中间件
let checkCodeMiddleware = (req, res, next) => {
  // 判断URL中是否code参数等于521
  if (req.query.code == 521) {
    next();
  } else {
    res.send('暗号错误');
  }
}

app.get('/admin', checkCodeMiddleware, (res, req) => {
  req.send('后台首页')
})

app.get('/setting', checkCodeMiddleware, (res, req) => {
  req.send('设置页面')
})

app.all('/*', (req, res) => {
  res.send('<h1>404 not Found</h1>')
})

// 4.监听端口，启动服务
app.listen(9000, () => {
  console.log('服务器启动成功');
});